package uit.shoeshop.action;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import uit.shoeshop.dto.CategoryDTO;
import uit.shoeshop.dto.ProductDTO;
import uit.shoeshop.lib.Constants;
import uit.shoeshop.lib.Util;
import uit.shoeshop.model.CategoryModel;

import com.opensymphony.xwork2.ActionSupport;
public class CategoryAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private ArrayList<ProductDTO> cateProduct;
	private String categoryTitle;
	private int cid;
	private int page;
	private String pagination;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
    public String execute() throws Exception {
    	CategoryModel category = new CategoryModel();
    	try{
    		setCateProduct(category.getAllProductbyCateID(getCid(), getPage()==0?1:getPage()));
    		setCategoryTitle(category.getTitleCategory(getCid()));
    		if(getCategoryTitle()==null) return ERROR;
    		setPagination(Util.Panigation("/shoeshop/category.html?cid="+getCid()+"&page=", category.countTotalProduct(getCid()), Constants.LIMIT_CATEGORY, getPage()==0?1:getPage()));
    	}catch(Exception e){
    		return ERROR;
    	}finally{
    		category = null;
    	}
    	
        return SUCCESS; //ERROR
    }

	public void setCateProduct(ArrayList<ProductDTO> cateProduct) {
		this.cateProduct = cateProduct;
	}

	public ArrayList<ProductDTO> getCateProduct() {
		return cateProduct;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public int getCid() {
		return cid;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	public void setCategoryTitle(String categoryTitle) {
		this.categoryTitle = categoryTitle;
	}

	public String getCategoryTitle() {
		return categoryTitle;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	public String getPagination() {
		return pagination;
	}
}
